package com.example.javawebdemo.dao;

//import com.example.javawebdemo.models.Product;
//import com.example.javawebdemo.jdbc.JDBCTemplateSingleton;
//import org.springframework.jdbc.core.BeanPropertyRowMapper;
//import org.springframework.jdbc.core.RowMapper;
//import java.sql.SQLException;
//import java.time.LocalDateTime;
//import java.util.List;
//public class ProductDAO {
//    private static final RowMapper<Product> PRODUCT_ROW_MAPPER =
//            new BeanPropertyRowMapper<>(Product.class);
//    public List<Product> getAllProducts() throws SQLException {
//        return JDBCTemplateSingleton.getJdbcTemplate().query(
//                "SELECT id, name, price, stock, description, image_url, created_at FROM products",
//                PRODUCT_ROW_MAPPER);
//    }
//    public Product getProductById(int id) throws SQLException {
//        try {
//            return JDBCTemplateSingleton.getJdbcTemplate().queryForObject(
//                    "SELECT * FROM products WHERE id = ?",
//                    PRODUCT_ROW_MAPPER, id);
//        } catch (Exception e) {
//            return null; // 查询无结果时返回null
//        }
//    }
//    public boolean addProduct(Product product) throws SQLException {
//        String sql = "INSERT INTO products (name, price, stock, description, image_url, created_at) " +
//                "VALUES (?, ?, ?, ?, ?, ?)";
//        return JDBCTemplateSingleton.getJdbcTemplate().update(sql,
//                product.getName(),
//                product.getPrice(),
//                product.getStock(),
//                product.getDescription(),
//                product.getImageUrl(),
//                LocalDateTime.now()) > 0;
//    }
//    public boolean updateProduct(Product product) throws SQLException {
//        String sql = "UPDATE products SET name=?, price=?, stock=?, description=?, image_url=? " +
//                "WHERE id=?";
//        return JDBCTemplateSingleton.getJdbcTemplate().update(sql,
//                product.getName(),
//                product.getPrice(),
//                product.getStock(),
//                product.getDescription(),
//                product.getImageUrl(),
//                product.getId()) > 0;
//    }
//    public boolean deleteProduct(int id) throws SQLException {
//        return JDBCTemplateSingleton.getJdbcTemplate().update(
//                "DELETE FROM products WHERE id = ?", id) > 0;
//    }
//}

// ProductDAO.java (接口)

import com.example.javawebdemo.models.Product;
import java.sql.SQLException;
import java.util.List;

public interface ProductDAO {
    List<Product> getAllProducts() throws SQLException;
    Product getProductById(int id) throws SQLException;
    boolean addProduct(Product product) throws SQLException;
    boolean updateProduct(Product product) throws SQLException;
    boolean deleteProduct(int id) throws SQLException;
}
